package com.vpa.graphics;

import java.util.HashMap;
import java.util.Map;

import javax.microedition.khronos.opengles.GL10;

import android.annotation.SuppressLint;
import android.graphics.Paint;
import android.graphics.Rect;


@SuppressLint("UseSparseArrays")
public class TextArea extends Spot 
{
	private HashMap<Integer, String> values = new HashMap<Integer, String>();
	private HashMap<Integer, String> titles = new HashMap<Integer, String>();
	private HashMap<Integer, String> units = new HashMap<Integer, String>();
	private int entries = 0;
	private Rect r = new Rect();
	private boolean newt = true;
	Paint tp;
	
	public void setValue(int index, String val)
	{
		if(values.containsKey(index))
			values.put(index, val);
	}
	
	public void removeEntry(int index)
	{
		values.remove(index);
		values.remove(titles);
		values.remove(units);
	}
	
	public int addEntry(String title, String units)
	{
		titles.put(entries, title);
		this.units.put(entries, units);
		values.put(entries, "");
		return entries++;
	}
	
	public TextArea()
	{
		text_size = 14;
	}
	
	public void draw(GL10 gl, GLView v)
	{
		if(newt || text == null || tp == null)
		{
			TPC ot;
			if((ot = GraphicsController.fontCache.get("Roboto-Regular.ttf" + text_size)) != null)
			{
				text = ot.text;
				tp = ot.p;
			}
			else
			{
				text = new GLText(gl, v.c.getAssets());
				tp = text.load("Roboto-Regular.ttf", text_size, 4, 4).p;
				GraphicsController.fontCache.put("Roboto-Regular.ttf" + text_size, new TPC(text, tp));
			}
		    newt = false;
		}
		gl.glBlendFunc( GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA );
		int summingHeights = 0;
		int maxowidth = 0;
		for(Map.Entry<Integer, String> e : values.entrySet())
		{
			String s = e.getValue();
			int i = e.getKey();
			String title = titles.get(i);
			String unit = units.get(i);
			String finstr = title + ": " + s + " " + unit;
			tp.getTextBounds(finstr, 0, finstr.length(), r);
			summingHeights += r.height();
			if(maxowidth < r.width())
				maxowidth = r.width();
			float r, g, b;
			r = g = b = 0;
			if(moving)
			{
				r = 0f; g = b = .45f;
			}
			drawText(r, g, b, 1f, finstr,
					x, -y - summingHeights + v.cheight, text);
		}
		setDims2(maxowidth, summingHeights);
		gl.glBlendFunc( GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA );
	}
	
	public void changeTextSize(int s)
	{
		if(text_size != s)
		{
			text_size = s;
			newt = true;
		}
	}
	
	protected void setDims(int w, int h, boolean up)
	{
		if(up)
			changeTextSize(text_size + 2);
		else
			changeTextSize(text_size - 2);
		super.setDims(w, h, false);
	}
	protected void setDims2(int w, int h)
	{
		super.setDims(w, h, false);
	}
}
